package com.hs.telepass.controller.task;


import android.content.Context;
import android.content.Intent;
import com.hs.telepass.controller.TaskController;
import com.hs.telepass.model.json.response.FetchPubkeyResponseJson;
import com.hs.telepass.utils.JsonUtil;

public class FetchPubkeyTask extends Task{
    public final static String ACTION_FETCH_PUBKEY_TASK = "action_fetch_pubkey_task";

    private Context mContext;
    private String jsonRequstStr;
    private String taskUrl;
    private int requestType;

    public FetchPubkeyTask(Context context, String enc_sessionId, String uuid){
        mContext = context;
        jsonRequstStr = "enc_sessionId=" + enc_sessionId + "&" + "udid=" + uuid;
        requestType = TASK_REQUST_TYPE_GET;
        taskUrl = BASE_API_URL + "/" + "bm/session/fetchPubkey";
    }

    @Override
    public void onExecuted(String response, int httpCode) {

        Intent intent = new Intent(getActionName());
        //处理奇葩json
        String cutJson = response.replaceAll("\\\\\"" , "\"").replaceAll("\"\\{", "{").replaceAll("\\}\"", "}");
        FetchPubkeyResponseJson responseJson = JsonUtil.fromJson(cutJson, FetchPubkeyResponseJson.class);
        String code = responseJson.getCode();

        if("200".equals(code)){
            intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_SUCCEED);
            intent.putExtra(TaskController.EXTRA_RESPONSE, responseJson);
        }

        broadcastManager.sendBroadcast(intent);
    }

    @Override
    public int getRequstType() {
        return requestType;
    }

    @Override
    public String getJsonRequstStr() {
        return jsonRequstStr;
    }

    @Override
    public int getResponseSuccessCode() {
        return 0;
    }

    @Override
    public String getTaskUrl() {
        return taskUrl;
    }

    @Override
    public String getActionName() {
        return ACTION_FETCH_PUBKEY_TASK;
    }
}
